package com.zhuss.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class Test3 {
	
	private static final String fileUrl = "E:\\workspaceMoudle\\workspace02\\wps";
	
	public static void main(String[] args) {
        //复制单级目录中的所有文件内容
        File file = new File("E:\\workspaceMoudle\\workspace01\\wps");//源文件
        File file2 = new File("E:\\workspaceMoudle\\workspace02\\wps");//目标文件
        file2.mkdir();//创建单级目录
        File[] files = file.listFiles();//文件集合
        
        copyFile(files, file2, file);

        System.out.println("文件复制完毕");
    }

	private static void copyFile(File[] files, File file2, File file) {
		String[] list = file.list();//文件名集合
		for (int i = 0; i < files.length; i++) {//遍历集合
			if (files[i].isFile()) {//如果是文件
	            File f2 = new File(file2, list[i]);//创建名字一样的文件
	            try {
	                // f2.createNewFile();//创建一个新文件
	                String name = f2.getAbsolutePath();
	                name = name.replace(".txt", ".java");
	                System.out.println(name);
	                f2 = new File(name);
	                //复制文件中的内容
	                try (Reader in = new FileReader(files[i]);
	                     Writer out = new FileWriter(f2)) {
	                    //创建字符数组
	                    char[] ch = new char[1024 * 1024];
	                    int len =0;
	                    while ((len = in.read(ch)) != -1) {
	                        out.write(ch,0,len);//将读取到的内容写入目标文件中
	                    }
	                } catch (FileNotFoundException e) {
	                    e.printStackTrace();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	
	        } else {
	        	System.out.println(file2.getAbsolutePath());
	        	String str = files[i].getAbsolutePath().substring(fileUrl.length());
	        	String file2Url = fileUrl + str;
	        	System.out.println(file2Url);
	        	File ft = new File(file2Url);
	        	ft.mkdir();
				copyFile(files[i].listFiles(), ft, files[i]);
			}
		} 
		
	}
	
	
}

